/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-03-12 10:02:58
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-13 09:26:38
 * @FilePath: /yt-devops/lib/pages/devices/charge_termimal_list/charge_termimal_list_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:get/get.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/pages/devices/device_controll/page_component/charge_terminal/charge_termimal_list/charge_termimal_list_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

class ChargeTermimalListController extends GetxController {
  ///
  DeviceServices services = Get.find<DeviceServices>();

  /// 充电桩id
  late int chargingId;

  /// 终端列表
  List<ChargeTerminalModel> terminalList = [];

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  initData(int deviceId) {
    chargingId = deviceId;
    getTerm();
  }

  getTerm() => Configs.httpClient.requestWithLoading(() async {
        List data =
            await services.getChargeTerm({'deviceId': chargingId}) ?? [];
        terminalList = data
            .map<ChargeTerminalModel>((e) => ChargeTerminalModel.fromJson(e))
            .toList();
        update();
      });

  addChargeEnd() {
    Get.toNamed(RouterPath.addChargeEnd, arguments: {'chargingId': chargingId})
        ?.then((value) {
      if (value == true) {
        getTerm();
      }
    });
  }

  toTel(ChargeTerminalModel data) {
    Get.toNamed(RouterPath.addChargeEnd, arguments: {'termimal': data})
        ?.then((value) {
      if (value == true) {
        getTerm();
      }
    });
  }

  deleteDeviceHandle(int id) {
    BrnDialogManager.showConfirmDialog(Get.context!,
        cancel: ResKey.cancel.tr,
        confirm: ResKey.confirm.tr,
        themeData: BrnDialogConfig(
            contentTextStyle:
                BrnTextStyle.withStyle(TextStyles.f18.mediumStyle)),
        message: ResKey.commonConfirmDelete.tr, onCancel: () {
      Get.back();
    }, onConfirm: () {
      Get.back();
      deleteDeviceRequest(id);
    });
  }

  deleteDeviceRequest(int id) =>
      Configs.httpClient.requestWithLoading(() async {
        await DeviceServices().delTerm({'deviceId': id});
        await FuncUtils.requestTipsDislog(
            Get.context!, ResKey.commonDelSuccess.tr, true,
            duration: const Duration(seconds: 1));
        getTerm();
      });

  bindMainService(int deviceId, bool isBind) =>
      Configs.httpClient.requestWithLoading(() async {
        await services
            .updateChargeTerm({'bindTermId': deviceId, 'isBind': isBind});

        getTerm();
      });
}
